package Controller.Actions;

import Controller.Action;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;

import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

import InformationRetriever.*;

import Database.DbConstants;
import Database.Document;
import Database.DocumentVector;
import Database.CommonOperations;

import java.util.List;
import java.util.Vector;

public class CompareAction implements Action {
    
    public CompareAction() {
    }
    
    public void perform(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache");
        
        String doc1 = request.getParameter("doc1");
        String doc2 = request.getParameter("doc2");
        String formula = request.getParameter("formula");

        ///retrieve document list from db...
        ObjectContainer db = Db4o.openFile(DbConstants.DB_FILE_NAME); 
        DocumentVector list = null;
        try
        {
            list = CommonOperations.getSortedDocuments(db);
            request.setAttribute("list", list);
        }
        catch(Exception e)
        {
            System.out.print("CompareAction.perform()");
            e.printStackTrace();
        }
        finally
        {
            db.close();
        }        
        
        if(doc1 == null || doc2 == null || formula == null)
        {
            RequestDispatcher rd = request.getRequestDispatcher("/compare/startcompare.jsp");
            rd.forward(request, response);
            return;
        }
            
        IComparer comparerMethod = ComparerFactory.FactoryMethod(formula);
        double value = comparerMethod.PerformCompare(Integer.parseInt(doc1), Integer.parseInt(doc2));
        request.setAttribute("value", value);
        request.setAttribute("doc1", list.get(Integer.parseInt(doc1)));
        request.setAttribute("doc2", list.get(Integer.parseInt(doc2)));
        
        String result = "";
        if(     formula.equals(Formulas.InnerProduct.toString())
                || formula.equals(Formulas.Cosine.toString())
                || formula.equals(Formulas.DiceCoefficient.toString())
                || formula.equals(Formulas.JaccardCoefficient.toString()))
        {
            value = Math.abs(value);
            if(value <= 0.25)
                result = "Not similar";
            else if(value <= 0.75)
                result = "Somehow similar";
            else
                result = "Very similar";
        }
        else
        {
            if(value <= 0.25)
                result = "Very similar";
            else if(value <= 0.75)
                result = "Somehow similar";
            else
                result = "Not similar";
        }
        request.setAttribute("result", result);
        
        RequestDispatcher rd = request.getRequestDispatcher("/compare/resultcompare.jsp");
        rd.forward(request, response);
    }
}
